package com.wzxy.nc.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;

import com.opensymphony.xwork2.ActionSupport;

public abstract class AbsBaseController extends ActionSupport{

	/**  */
	private static final long serialVersionUID = -3797879860329776853L;
	
	/** 需要跳转的页面链接 */
	protected String forwardPage;
	
	protected final String DOWNLOAD = "download";
	
	protected final String JSON = "json";
	
	protected Map<String,Object> dataMap;
	
	// 下载的excel文件名
	protected String downFileName;
	// 下载的excel输入流
	protected InputStream fileInputStream;
	
	protected HttpServletResponse response;
	
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}
	
	public HttpSession getSession() {
		return getRequest().getSession();
	}
	
    public HttpServletResponse getResponse() {
    	if(null == response){
    		response = ServletActionContext.getResponse();
            response.setContentType("application/json;charset=UTF-8");
            response.setHeader("Cache-Control","no-cache"); 
    	}
		return response;
	}
    
    /**
     * 
     * <p>Description: 下载excel</p>
     * <p>Company: 梧州学院</p> 
     * @author: 胡宇鹏
     * @date: 2018年4月27日 下午2:27:08
     * @param list
     * @param cls
     * @param fileName
     * @return
     */
	@SuppressWarnings("rawtypes")
	public String download(List list, Class cls, String fileName){
		try {
			Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(
					fileName,fileName), cls, list);
			// 第七步，将文件存到流中
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			workbook.write(os);
			byte[] fileContent = os.toByteArray();
			ByteArrayInputStream is = new ByteArrayInputStream(fileContent);
			// 文件流
			fileInputStream = is; 
			// 设置下载的文件名,解决无法显示中文名字的问题
			String excel = fileName + ".xls";
			downFileName = new String(excel.getBytes("gb2312"), "iso8859-1");
		} catch (IOException e) {
			e.printStackTrace();
		}	
		return DOWNLOAD;
	}
	
	public String getForwardPage() {
		return forwardPage;
	}

	public void setForwardPage(String forwardPage) {
		this.forwardPage = forwardPage;
	}

	public String getDownFileName() {
		return downFileName;
	}

	public void setDownFileName(String downFileName) {
		this.downFileName = downFileName;
	}

	public InputStream getFileInputStream() {
		return fileInputStream;
	}

	public void setFileInputStream(InputStream fileInputStream) {
		this.fileInputStream = fileInputStream;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

}
